﻿/*
 * Created by SharpDevelop.
 * User: 1
 * Date: 09.08.2013
 * Time: 10:23
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Region.Forms
{
	/// <summary>
	/// Description of IPForm.
	/// </summary>
	public partial class IPForm : Form
	{
	  IP m_ip;
	  Engine m_engine;
	  Boolean m_NewIP;
	  void Read()
	  {
	    if (m_ip==null) return;
	    INN.ReadOnly = !m_NewIP;
	    OGRN.ReadOnly = !m_NewIP;
	    FIO.ReadOnly = !m_NewIP;
	    if (m_NewIP)
	    {
	      INN.BackColor = SystemColors.ButtonFace;
	    }
			INN.Text = m_ip.INN;
			OGRN.Text = m_ip.OGRN;
			FIO.Text = m_ip.FIO;
			Telefon.Text = m_ip.Telefon;
			Post.Text = m_ip.Post;
			Region.Text = m_ip.Region;
			Raion.Text = m_ip.Raion;
			Naspunkt.Text = m_ip.Naspunkt;
			Street.Text = m_ip.Street;
			Dom.Text = m_ip.Dom;
			Korpus.Text = m_ip.Korpus;
			Kvart.Text = m_ip.Kvart;	    
	  }
	  /// <summary>Сохранить изменения</summary>
	  void Write()
	  {
	    if (m_ip==null) return;
			m_ip.Telefon = Telefon.Text;
			m_ip.Post = Post.Text ;
			m_ip.Region = Region.Text;
			m_ip.Raion = Raion.Text;
			m_ip.Naspunkt = Naspunkt.Text;
			m_ip.Street = Street.Text;
			m_ip.Dom = Dom.Text;
			m_ip.Korpus = Korpus.Text;
			m_ip.Kvart = Kvart.Text;
			m_engine.bd_IP.SubmitChanges();
	  }
	  public IPForm(Engine engine,IP ip):this(engine,ip,false)
	  {
	  }
	  /// <summary>Добавить нового ИП если такой есть открыть для редактирования</summary><param name="engine">Основной объект программы</param><param name="INN">ИНН</param><param name="OGRN">ОГРН</param><param name="FIO">ФИО ИП</param>
	  public IPForm(Engine engine,String INN,String OGRN,String FIO):this(engine,null,false)
	  {
	    IEnumerable<IP> table = (from t in engine.bd_IP.IP where t.INN==INN && t.OGRN==OGRN select t);
	    if (table.Any())
	    {
	      m_ip = table.First();
	    }
	    else
      {
	      m_ip = new Region.IP();
	      m_ip.INN = INN;
	      m_ip.OGRN = OGRN;
	      engine.bd_IP.IP.InsertOnSubmit(m_ip);	      
	    }
	    Read();
	  }
		public IPForm(Engine engine,IP ip,Boolean NewIP)
		{
		  m_ip = ip;
		  m_engine = engine;
		  m_NewIP = NewIP; 
			InitializeComponent();
			Read();
		}
		void SaveButtonClick(object sender, EventArgs e)
		{
      Write();	  
		}
	}
}
